﻿
using System;

namespace GNUGo.Net.CLI.CliControls {
    public abstract class CliControl {
        private int _left;
        private int _top;
        private int _width;
        private int _height;
        private ConsoleColor _forecolor;
        private ConsoleColor _backcolor;
        public event PaintEventHandler Paint;

        protected CliControl() {
            _forecolor = ConsoleColor.Gray;
            _backcolor = ConsoleColor.Black;
            Paint += OnPaint;
        }

        protected virtual void OnPaint(object sender, PaintEventArgs args) {
        }

        public virtual void Invalidate() {
            if (Paint != null) {
                CliGraphics g = new CliGraphics(Width, Height, Forecolor, Backcolor);
                PaintEventArgs e = new PaintEventArgs(g);
                Paint(this, e);
                g.DrawToScreen(Left, Top);
            }
        }

        public void PaintToBuffer(CliGraphics graphics) {
            CliGraphics g = new CliGraphics(Width, Height, Forecolor, Backcolor);
            PaintEventArgs e = new PaintEventArgs(g);
            Paint(this, e);
            graphics.DrawGraphics(g, Left, Top);
        }

        public int Left {
            get { return _left; }
            set {
                if (_left != value) {
                    _left = value;
                    Invalidate();
                }
            }
        }

        public int Top {
            get { return _top; }
            set {
                if (_top != value) {
                    _top = value;
                    Invalidate();
                }
            }
        }

        public int Width {
            get { return _width; }
            set {
                if (_width != value) {
                    _width = value;
                    Invalidate();
                }
            }
        }

        public int Height {
            get { return _height; }
            set {
                if (_height != value) {
                    _height = value;
                    Invalidate();
                }
            }
        }

        public ConsoleColor Forecolor {
            get { return _forecolor; }
            set {
                if (_forecolor != value) {
                    _forecolor = value;
                    Invalidate();
                }
            }
        }

        public ConsoleColor Backcolor {
            get { return _backcolor; }
            set {
                if (_backcolor != value) {
                    _backcolor = value;
                    Invalidate();
                }
            }
        }
    }
}
